package com.csii.activiti.server.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.activiti.server.api.vo.Result;
import com.csii.activiti.server.core.entity.*;
import com.csii.activiti.server.core.query.*;
import com.csii.activiti.server.core.service.FlowQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="流程查询接口")
@RestController
@RequestMapping("/${activity.basic.url:activiti}/flow/query")
public class FlowQueryController {
    @Autowired
    private FlowQueryService flowQueryService;

    @ApiOperation("查询待办列表")
    @GetMapping("/todoList")
    public Result<Page<ActFlowTodo>> queryTodoList(QueryActFlowTodo queryActFlowTodo,
                                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        return Result.ok(flowQueryService.queryTodoList(queryActFlowTodo,new Page(pageNo, pageSize),null));
    }

    @ApiOperation("查询已办列表")
    @GetMapping("/doneList")
    public Result<Page<ActFlowDone>> queryDoneList(QueryActFlowDone queryActFlowDone,
                                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        return Result.ok(flowQueryService.queryDoneList(queryActFlowDone,new Page(pageNo, pageSize)));
    }

    @ApiOperation("查询我的申请列表")
    @GetMapping("/myApplyList")
    public Result<Page<ActFlowMyApply>> queryMyApplyList(QueryActFlowMyApply queryActFlowMyApply,
                                                         @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        return Result.ok(flowQueryService.queryMyApplyList(queryActFlowMyApply,new Page(pageNo, pageSize)));
    }

    @ApiOperation("查询我的申请详情")
    @GetMapping("/myApplyFlowDetail")
    public Result<ActFlowDetail> queryMyApplyFlowDetail(@RequestParam String processInstanceId){
        return Result.ok(flowQueryService.queryMyApplyFlowDetail(processInstanceId));
    }

    @ApiOperation("查询待办详情")
    @GetMapping("/flowDetail")
    public Result<ActFlowDetail> queryFlowDetail(@RequestParam String id){
        return Result.ok(flowQueryService.queryFlowDetail(id));
    }

    @ApiOperation("查询已办详情")
    @GetMapping("/doneFlowDetail")
    public Result<ActFlowDetail> doneFlowDetail(@RequestParam String taskId){
        return Result.ok(flowQueryService.queryFlowDetail(taskId));
    }

    @ApiOperation("查询流程申请列表")
    @GetMapping("/applyFlowList")
    public Result<List<ActFlowGroup>> queryApplyFlowList(){
        return Result.ok(flowQueryService.queryMaintainFlowList(true));
    }

    @ApiOperation("查询流程维护列表")
    @GetMapping("/maintainFlowList")
    public Result<List<ActFlowGroup>> queryMaintainFlowList(){
        return Result.ok(flowQueryService.queryMaintainFlowList(false));
    }


    @ApiOperation("查询流程权限")
    @GetMapping("/flowPermission")
    public Result<ActFlowPermission> queryFlowPermission(@ApiParam("流程ID") @RequestParam String flowId){
        return Result.ok(flowQueryService.queryFlowPermission(flowId));
    }

    @ApiOperation("查询流程配置")
    @GetMapping("/flowConfiguration")
    public Result<ActFlowConfiguration> queryFlowConfiguration(@ApiParam("流程ID")@RequestParam String flowId){
        return Result.ok(flowQueryService.queryFlowConfiguration(flowId));
    }

    @ApiOperation("查询流程申请详情")
    @GetMapping("/flowApply")
    public Result<ActFlowApply> queryFlowApply(@ApiParam("流程key")@RequestParam String flowKey){
        return Result.ok(flowQueryService.queryFlowApply(flowKey));
    }


    @ApiOperation("通过任务ID查询签名")
    @GetMapping("/digitalSignatureByTaskId")
    public Result<String> digitalSignatureByTaskId(@ApiParam("任务ID") @RequestParam String taskId){
        return Result.ok(flowQueryService.queryDigitalSignatureByTaskId(taskId));
    }


    @ApiOperation("通过秘钥查询流程实列")
    @GetMapping("/flowInstanceByKey")
    public Result<List<ActFlowInstance>> queryFlowInstanceByKey(@ApiParam("key") @RequestParam String key){
        return Result.ok(flowQueryService.queryFlowInstanceByKey(key));
    }

    @ApiOperation("查询已转交记录")
    @GetMapping("/transferRecordList")
    public Result<Page<ActFlowTransferRecord>> queryTransferRecordList(QueryActFlowTransferRecord queryActFlowTransferRecord,
                                                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        return Result.ok(flowQueryService.queryTransferRecordList(queryActFlowTransferRecord,new Page(pageNo, pageSize)));
    }

    @ApiOperation("查询执行流程（通过部署ID）")
    @PostMapping("/queryExecutionStream")
    public Result<List<ActFlowNode>> queryExecutionStream(@RequestBody QueryActExecutionStream queryActExecutionStream){
        return Result.ok(flowQueryService.queryExecutionStream(queryActExecutionStream.getDeploymentId(),queryActExecutionStream.getFormData()));
    }

    @ApiOperation("查询执行流程（通过流程ID）")
    @PostMapping("/queryExecutionStreamByFlowId")
    public Result<List<ActFlowNode>> queryExecutionStreamByFlowId(@RequestBody QueryActExecutionStream queryActExecutionStream){
        return Result.ok(flowQueryService.queryExecutionStreamByFlowId(queryActExecutionStream.getFlowId(),queryActExecutionStream.getFormData()));
    }

    @ApiOperation("查询流程申请详情(通过流程ID)")
    @GetMapping("/flowApplyByFlowId")
    public Result<ActFlowApply> queryFlowApplyByFlowId(@ApiParam("流程Id")@RequestParam String flowId){
        return Result.ok(flowQueryService.queryFlowApplyByFlowId(flowId));
    }

    @ApiOperation("查询流程表单定义")
    @GetMapping("/flowFormDefinitionByFlowKey")
    public Result<Object> queryFlowFormDefinitionByFlowKey(@ApiParam("流程Key")@RequestParam String flowKey){
        return Result.ok((Object) flowQueryService.queryFlowConfigurationByFlowKey(flowKey).getFormJson());
    }


    @ApiOperation("查询流程审批历史(通过流程实例ID)")
    @GetMapping("/approvalHistory")
    public Result<List<ActFlowApprovalHistory>> queryApprovalHistory(@ApiParam("流程实例ID")@RequestParam String processInstanceId,@ApiParam("是否只查询审批人")@RequestParam(required = false) boolean isQueryToApprove){
        return Result.ok(flowQueryService.queryApprovalHistory(processInstanceId,isQueryToApprove));
    }

    @ApiOperation("查询流程详情历")
    @GetMapping("/queryFlowDetailHistory")
    public Result<List<ActFlowDetail>> queryFlowDetailHistory(@ApiParam("流程实例ID")@RequestParam String processInstanceId){
        return Result.ok(flowQueryService.queryFlowDetailHistory(processInstanceId));
    }
}
